//
//  MainVC.m
//  KeTianDemo
//
//  Created by jay on 2020/8/24.
//  Copyright © 2020 jay. All rights reserved.
//

#import "MainVC.h"
#import "MainVM.h"
#import "MainNavView.h"
#import "CardView.h"
#import "CardItemCell.h"
#import "UIImageView+TXWebImage.h"

//自动请求下一页数据位置索引
#define kAutoRequestNextPageDataIndex    6

@interface MainVC ()<CardViewDataSource, CardViewDelegate>

@property (nonatomic, strong) MainVM *curVM;
@property (nonatomic, strong) MainNavView *myNavView;
@property (nonatomic, strong) CardView *cardView;//!< 主角：CardView

@property (nonatomic, assign) BOOL isLoading; //是否正在请求数据

@end

@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
            
    self.isLoading = NO;
    
    self.view.backgroundColor = TX_COLOR_FROM_RGB(0xEDEDED);

    [self.cardView registerClass:[CardItemCell class] forCellReuseIdentifier:kTableCellID];
    
    //请求数据
    [self reqGetData:YES isReqMore:NO];
}

//返回状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}

#pragma mark - Request Method

//开始搜索
-(void)begineSearch:(NSString *)keywords {
    
    [[[UIApplication sharedApplication].delegate window] endEditing:YES];
        
    self.curVM.keywords = keywords;
    
    //请求数据
    [self reqGetData:YES isReqMore:NO];
}

//请求列表数据
-(void)reqGetData:(BOOL )isShowLoading isReqMore:(BOOL )isReqMore  {
    
    if (self.isLoading) {
        return;
    }
    self.isLoading = YES;
    
    if (isShowLoading) {
        [self showLoading];
    }
    
    TX_WS(weakSelf);
    [self.curVM getDataFromNet:isReqMore completeHandle:^(NSError *error) {
        
        if (isShowLoading) {
            [weakSelf hideLoading];
        }
        
        weakSelf.isLoading = NO;

        if (error) {
            TX_TOAST(error.localizedDescription);
            return;
        }
        
        [weakSelf.cardView reloadData];
    }];
}

#pragma mark - CardViewDataSource

- (CardItemCell *)cardView:(CardView *)cardView cellForRowAtIndex:(NSInteger)index {
    
    CardItemCell *cell = [cardView dequeueReusableCellWithIdentifier:kTableCellID];
    
    ImageListModel *model_ =[self.curVM modelForRow:index];
    
    [cell.picImageView tx_setImageWithURL:[NSURL URLWithString:model_.img] placeholderImage:[UIImage imageNamed:@"default_img_card"]];
    cell.titleLabel.text = model_.title;

    return cell;
}

- (NSInteger)numberOfCountInCardView:(UITableView *)cardView {
    return [self.curVM rowNumber];
}

#pragma mark - QCardViewDelegate

- (void)cardView:(CardView *)cardView didRemoveLastCell:(CardViewCell *)cell forRowAtIndex:(NSInteger)index {
    [cardView reloadDataAnimated:YES];
}

- (void)cardView:(CardView *)cardView didRemoveCell:(CardViewCell *)cell forRowAtIndex:(NSInteger)index {
    TXLog(@"didRemoveCell index = %ld", index);
    
    //滑动到倒数第kAutoRequestNextPageDataIndex个时，自动请求下一页数据
    if (index == self.curVM.dataArr.count-kAutoRequestNextPageDataIndex) {
        
        //如果有下一页，后台自动加载下一页数据
        if (self.curVM.hasNext) {
            
            //请求数据
            [self reqGetData:NO isReqMore:YES];
        }
    }
}

- (void)cardView:(CardView *)cardView didDisplayCell:(CardViewCell *)cell forRowAtIndex:(NSInteger)index {
    
    TXLog(@"didDisplayCell index = %ld, currentFirstIndex:%ld", index, cardView.currentFirstIndex);
}

- (void)cardView:(CardView *)cardView didMoveCell:(CardViewCell *)cell forMovePoint:(CGPoint)point {
}

#pragma mark - Getter

- (MainVM *)curVM {
    if(_curVM == nil) {
        _curVM = [[MainVM alloc] init];
    }
    return _curVM;
}

- (MainNavView *)myNavView {
    if(_myNavView == nil) {
        _myNavView = [[MainNavView alloc] init];
        [_myNavView.searchView setPlaceholderText:@"请输入搜索内容" placeholderColor:TX_COLOR_FROM_RGB(0xFFFFFF)];
        _myNavView.searchView.inputTF.text = self.curVM.keywords;
        [self.view addSubview:_myNavView];
        
        [_myNavView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(TX_NAVBAR_HEIGHT);
        }];
        
        TX_WS(weakSelf);
        _myNavView.searchView.searchTextBlock = ^(NSString * _Nonnull text) {
            
            [weakSelf begineSearch:text];
        };
    }
    return _myNavView;
}

- (CardView *)cardView {
    if(_cardView == nil) {
        _cardView = [[CardView alloc] init];
        _cardView.dataSource = self;
        _cardView.delegate = self;
        _cardView.visibleCount = 3;
        _cardView.lineSpacing = 15.0;
        _cardView.interitemSpacing = 10.0;
        _cardView.maxAngle = 10.0;
        _cardView.isAlpha = YES;
        _cardView.maxRemoveDistance = 100.0;
        _cardView.layer.cornerRadius = 10.0;
        [self.view addSubview:_cardView];
        
        [_cardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
            make.top.mas_equalTo(self.myNavView.mas_bottom).mas_equalTo(60);
            make.height.mas_equalTo(500);
        }];
    }
    return _cardView;
}

@end
